
# https://dianying.taobao.com/
# 保存到本地 再解析

import urllib.request
import json
import jsonpath

def create_headers(url):
    headers = {
        'Accept':'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
        'Referer':'https://dianying.taobao.com/',
        'Cookie': 'cna=VKkDIM5qu0wCAa+nhEdGSl2P; tfstk=gud6zSgAmlq6SHOSrc3FVfpL8MCj4FGzhr_vrEFak1C9GstwYtBwki5XcntfbtnG_ItCsBJagGoGGEKPG0orab8MS_fx40rA5qIVMZhVDvWtZu7GB0oraX8MSsfx4hr4vhLCPaCAWtCvJ2_AuseTBnIdpaQzXsCvD2TdxZCADnCxR6hoSj_nKgYs5yVfFTkOVFIQMSdKNOsWW-PYMB_1CRYOvF8wON6129vXSMd9YEdws9DUKsYeFn9v2DyNXL_WATJsV5tWjZKdJhu4nw9XkQfMjmNd2LtA9Cp7kSLC1GtW1OqsAgdMJQfdLb3WJBxwSBT4kjQe4MLM6sG-r11OX6_HgDVOcppv_Of0XmsDOed9hg5zag_ty-wcXS_C42gQn-4nUaESUuN8cOQh8huIR--1BwbC42gQn-XO-w-ZR2w2f; t=b848cb0e3d7f752f9e81866b7ae81d4a; cookie2=1c351e6c06055b707108f79a8041a412; v=0; _tb_token_=7e7bde94e38e7; xlly_s=1; isg=BIuL32TeaMDpkbSUDdGUDnkcGi91IJ-ifyjbMv2IO0ohHKp-hPL38nr_9hzyPPea',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url=url,headers=headers)
    return request



def daili(request):
    # 代理
    proxies = {
        'http': '101.200.75.55:80' # 北京
    }
    # handler
    handler = urllib.request.ProxyHandler(proxies=proxies)
    opener = urllib.request.build_opener(handler)
    response = opener.open(request)
    return response


def get_content(response):
    content = response.read().decode('utf-8')
    # print(content)
#   jsonp20({"returnCode":"0","returnValue":{}});  反扒
#   split
    content = content.split('(')[1].split(')')[0]
    return  content

def download(content):
    f = open('./download/taopiaopiao.json','w',encoding='utf-8')
    f.write(content)
    f.close()
    print('下载完成')

def myJsonPath():
    j_load = json.load(open('./download/taopiaopiao.json','r',encoding='utf-8'))
    city_list = jsonpath.jsonpath(j_load,'$..regionName')
    print(city_list)


if __name__ == '__main__':
    url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1737680522848_104&jsoncallback=jsonp105&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
#     定制请求头
    request = create_headers(url)
    response = daili(request)
    content = get_content(response)
    # download(content)
    myJsonPath()
